package com.example.joblinker.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.common.Result;
import com.example.joblinker.dto.CompanyDTO;
import com.example.joblinker.service.CompanyService;
import com.example.joblinker.util.JwtUtil;
import com.example.joblinker.vo.CompanyVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/companies")
public class CompanyController {

    @Resource
    private CompanyService companyService;

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 创建公司
     */
    @PostMapping
    public Result<CompanyVO> createCompany(@RequestBody @Valid CompanyDTO companyDTO, HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        CompanyVO vo = companyService.createCompany(companyDTO, userId);
        return Result.success(vo);
    }

    /**
     * 获取当前用户的公司信息
     */
    @GetMapping("/current")
    public Result<CompanyVO> getCurrentCompany(HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        CompanyVO vo = companyService.getCompanyByUserId(userId);
        return Result.success(vo);
    }

    /**
     * 获取公司详情
     */
    @GetMapping("/{id}")
    public Result<CompanyVO> getCompanyDetail(@PathVariable Long id) {
        CompanyVO vo = companyService.getCompanyDetail(id);
        return Result.success(vo);
    }

    /**
     * 更新公司信息
     */
    @PutMapping("/{id}")
    public Result<CompanyVO> updateCompany(@PathVariable Long id, @RequestBody @Valid CompanyDTO companyDTO, HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        CompanyVO vo = companyService.updateCompany(id, companyDTO, userId);
        return Result.success(vo);
    }

    /**
     * 获取公司列表
     */
    @GetMapping
    public Result<Page<CompanyVO>> getCompanyList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<CompanyVO> result = companyService.getCompanyList(page, size);
        return Result.success(result);
    }

    /**
     * 获取公司发布的职位
     */
    @GetMapping("/{id}/jobs")
    public Result<Page<com.example.joblinker.vo.JobVO>> getCompanyJobs(
            @PathVariable Long id,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<com.example.joblinker.vo.JobVO> result = companyService.getCompanyJobs(id, status, page, size);
        return Result.success(result);
    }
} 